package com.zy.experience_system.service.impl;

import com.zy.experience_system.entity.StuInformation;
import com.zy.experience_system.entity.TeachInformation;
import com.zy.experience_system.entity.User;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.enums.RoleEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.AccountForm;
import com.zy.experience_system.form.CreateAccountForm;
import com.zy.experience_system.repository.StuInformationRepository;
import com.zy.experience_system.repository.TeachInformationRepository;
import com.zy.experience_system.repository.UserRepository;
import com.zy.experience_system.service.AccountService;
import com.zy.experience_system.util.JwtUtil;
import com.zy.experience_system.util.MD5Util;
import com.zy.experience_system.util.MyStringUtil;
import com.zy.experience_system.util.ResultUtil;
import com.zy.experience_system.vo.AccountNameVO;
import com.zy.experience_system.vo.AccountVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private StuInformationRepository stuInformationRepository;

    @Autowired
    private TeachInformationRepository teachInformationRepository;

    @Override
    public AccountVO login(AccountForm accountForm) {
        User user = userRepository.findByUsername(accountForm.getUsername());
        if (user == null)
            throw new CommonException(400, "账号不存在");
        if (!user.getPassword().equals(MD5Util.getMD5(accountForm.getPassword()))) {
            throw new CommonException(400, "密码不正确");
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("role", user.getRole());
        map.put("perms", user.getPerms());
        String jwt = jwtUtil.createJwt(user.getUsername(), map);
        AccountVO accountVO = new AccountVO();
        accountVO.setToken(jwt);
        accountVO.setUsername(user.getUsername());
        accountVO.setPassword(user.getPassword());
        accountVO.setRole(user.getRole());
        accountVO.setPerms(user.getPerms());
        return accountVO;
    }

    @Override
    public AccountVO findByUsername(String username) {
        User user = userRepository.findByUsername(username);
        if (user == null)
            throw new CommonException(400, "账号不存在");
        AccountVO accountVO = new AccountVO();
        BeanUtils.copyProperties(user,accountVO);
        return accountVO;
    }

    @Override
    public User findUserByUsername(String username) {
        User user = userRepository.findByUsername(username);
        return user;
    }

    @Override
    public void save(CreateAccountForm createAccountForm) {
//        User user1 = userRepository.findByUsername(username);
        String username = createAccountForm.getUsername();
        String password = createAccountForm.getPassword();
        String role = createAccountForm.getRole();
        String perms = createAccountForm.getPerms();
        String name = createAccountForm.getName();
        List<User> users = userRepository.findAllByUsername(username);
        if (users.size()==0){
            User user = new User();
            user.setUsername(username);
            user.setPassword(MD5Util.getMD5(password));
            user.setRole(role);
            user.setPerms(perms);
            userRepository.save(user);
            if (role.equals(RoleEnum.STUDENT.getRole())){
                StuInformation stuInformation = new StuInformation();
                stuInformation.setStuId(username);
                stuInformation.setStuName(name);
                stuInformationRepository.save(stuInformation);
            }
            else if (role.equals(RoleEnum.TEACHER.getRole())){
                TeachInformation teachInformation = new TeachInformation();
                teachInformation.setTeachId(username);
                teachInformation.setTeachName(name);
                teachInformationRepository.save(teachInformation);
            }
            return;
        }
        throw new CommonException(ResultEnum.FAIL);


    }

    @Override
    public void update(String username,String password) {
        User user = userRepository.findByUsername(username);
        user.setPassword(password);
        userRepository.save(user);
    }

    @Override
    public void saveStu(String students) {
        Map<String,String> stuMap = MyStringUtil.parse(students);
        for (String username : stuMap.keySet()) {
            if (userRepository.findByUsername(username)!=null){
                continue;
            }
            String stuName = stuMap.get(username);
            User user = new User();
            user.setUsername(username);
            user.setPassword(MD5Util.getMD5(username));
            user.setRole("student");
            user.setPerms("normal");
            userRepository.save(user);
            StuInformation stuInformation = new StuInformation();
            stuInformation.setStuId(username);
            stuInformation.setStuName(stuName);
            stuInformationRepository.save(stuInformation);
        }
    }

    @Override
    public List<AccountNameVO> findAll() {
        List<User> userList = userRepository.findAll();
        List<AccountNameVO> accountNameVOList = new ArrayList<>();
        for (User user : userList) {
            AccountNameVO accountNameVO = new AccountNameVO();
            if (user.getRole().equals(RoleEnum.STUDENT.getRole())){
                if (stuInformationRepository.findAllByStuId(user.getUsername()).size()==1){
                    StuInformation stuInformation = stuInformationRepository.findByStuId(user.getUsername());
                    accountNameVO.setSId(stuInformation.getId());
                    accountNameVO.setName(stuInformation.getStuName());
                }
            }
            else if (user.getRole().equals(RoleEnum.TEACHER.getRole())){
                if(teachInformationRepository.findAllByTeachId(user.getUsername()).size()==1){
                    TeachInformation teachInformation = teachInformationRepository.findByTeachId(user.getUsername());
                    accountNameVO.setTId(teachInformation.getId());
                    accountNameVO.setName(teachInformation.getTeachName());
                }
            }
            else {
                accountNameVO.setName("管理员" + user.getUsername());
            }
            BeanUtils.copyProperties(user,accountNameVO);
            accountNameVOList.add(accountNameVO);
        }
        return accountNameVOList;
    }


}
